<?php
	/**
	 * 好友详细信息
	 */
    class GetFriendInfo extends APIBase {
		
		public $tag = "GetFriendInfo";
		public $isLogin = false;
		
		public function before() {
			$this->initMysql();
    		return true;
		}
		
		public function logic() {
			if (!isset($this->param["friend_uid"])) {
				$this->returnError(301,"friend_uid must be needed");
			} else {
				$friend_uid = $this->param["friend_uid"];
			}
			
			$ret = $this->mysql->query("select 1 from player where id = $friend_uid limit 1");
			
			if (!$ret)
			{
				$this->returnError(302,"该玩家不存在");
			}
			
			$this->initDataRedis($friend_uid);
			$key = $this->data_redis->keys("hu:$friend_uid");
			// redis内没有改玩家数据
			if (!count($key)) {
				//操作数据库
				$ret = $this->mysql->select("player", "*", array('id'=>$friend_uid));
				if (!count($ret)) {
				$this->returnError(303,"该UID不存在");
				}
				$user = array();
				$user = $ret[0];
				$total_board = $user['total_board'] ;
				$total_win =  $user['total_win'];
				$total_lose = $total_board - $total_win;
				$user_info = array(
						'uid' => (int) $friend_uid,
						'user' =>$user['user'],
						'name' => (string)$user['name'],
						'sex' => (int) $user['sex'],
						'money' => (int) $user['money'],
						'rmb' => (int) $user['rmb'],
						'coin' => (int) $user['coin'],
						'history' => array((int) $total_win, (int) $total_lose),
						'exp' => (int) (isset($user['exp']) ? $user['exp'] : 0),
						'avater_url' => $user['avater_url'],
						'gift_num1' =>(int) $user['gift_num1'],
						'gift_num2' =>(int) $user['gift_num2'],
						'gift_num3' =>(int) $user['gift_num3'],
						'gift_num4' =>(int) $user['gift_num4'],
						'charisma' => (int) $user['charisma'],
						'is_online' => (int)$user['is_online'],
						'tool1' => (int)$user['tool1'],
						'tool2' => (int)$user['tool2'],
						'tool3' => (int)$user['tool3']
				);							
			}	
			else
			{
				//操作缓存
				$user = $this->data_redis->hGetAll("hu:{$friend_uid}");
				$total_board = isset($user['total_board']) ? $user['total_board'] : 0;
				$total_win = isset($user['total_win']) ? $user['total_win'] : 0;
				$total_lose = $total_board - $total_win;
				$user_info = array(
						'uid' => (int) $friend_uid,
						'user' =>$user['user'],
						'name' => (string)$user['name'],
						'sex' => (int) $user['sex'],
						'money' => (int) $user['money'],
						'rmb' => (int) $user['rmb'],
						'coin' => (int) $user['coin'],
						'history' => array((int) $total_win, (int) $total_lose),
						'exp' => (int) (isset($user['exp']) ? $user['exp'] : 0),
						'avater_url' => $user['avater_url'],
						'gift_num1' => (int)$user['gift_num1'],
						'gift_num2' => (int)$user['gift_num2'],
						'gift_num3' => (int)$user['gift_num3'],
						'gift_num4' => (int)$user['gift_num4'],
						'charisma' => (int) $user['charisma'],
						'is_online' => (int)$user['isonline'],
						'tool1' => (int)$user['tool1'],
						'tool2' => (int)$user['tool2'],
						'tool3' => (int)$user['tool3']
				);
			}						
			$this->returnData($user_info);
		}
		
       	public function after() {
    		$this->deinitDataRedis();
    		$this->deinitMysql();
		}
    }
?>
